<?php
$all_files = glob("input/*.php");
$file_path = $_POST['path'];

if (isset($_POST['extract'])) {	
	$zipFileName = 'structure.zip';
	
//	$zipFolder = sys_get_temp_dir(); // must be writable
	$zipArchive = tempnam(sys_get_temp_dir(), '').'.zip';			
	
	$folders = array();
	if(preg_match('@\n@', $file_path)) {
		$file_path = nl2br($file_path);
		$folders = explode('<br />', $file_path);
	} else {
		$folders = array($file_path);
	}
	
	$extract_file = 0;
	$extract_module = 0;
	if(preg_match('@|@',$file_path)) {
		$exploded = explode("|", $file_path);
		$extract_file = trim($exploded['1']); 
		$file_path = trim($exploded['0']);
		
		if(preg_match('@module@',$file_path))
			$extract_module = 1;
	}
	
	// Create the Zip
	$zip = new ZipArchive();
	$zip->open($zipArchive, ZipArchive :: CREATE);	
// 	$zip->addEmptyDir($file_path);
 	
 	
 	
 	if($extract_file) { // file extration
 		
// 		echo $file_path." | ".$extract_file." | ".$extract_module;
//		die;
 		
 		if(!empty($extract_module)) { // module extraction
 			$docs_root = $_SERVER['DOCUMENT_ROOT'];
 			$file_path = preg_replace('@/(.+)@','', $file_path); 			 
 			$file_path = $docs_root."/".$file_path."/";

// 			$file_path = preg_replace('@/(.+)@','', $file_path); 			 
// 			$file_path = "../".$file_path."/";
			
//			var_dump('catalog/controller/module/'.$extract_file, file_get_contents($file_path.'catalog/controller/module/'.$extract_file));
//			die;
			
			// Catalog
			if(is_file($file_path.'catalog/controller/module/'.$extract_file))
 			$zip->addFromString('catalog/controller/module/'.$extract_file, file_get_contents($file_path.'catalog/controller/module/'.$extract_file));
 			
 			if(is_file($file_path.'catalog/language/english/module/'.$extract_file))
 			$zip->addFromString('catalog/language/english/module/'.$extract_file, file_get_contents($file_path.'catalog/language/english/module/'.$extract_file));
 			
 			// Admin
 			if(is_file($file_path.'admin/controller/module/'.$extract_file))
 			$zip->addFromString('admin/controller/module/'.$extract_file, file_get_contents($file_path.'admin/controller/module/'.$extract_file));
 			
 			if(is_file($file_path.'admin/language/english/module/'.$extract_file))
 			$zip->addFromString('admin/language/english/module/'.$extract_file, file_get_contents($file_path.'admin/language/english/module/'.$extract_file));
 			
 			$extract_file = str_replace('.php', '.tpl', $extract_file); 			
 			if(is_file($file_path.'admin/view/template/module/'.$extract_file))
 			$zip->addFromString('admin/view/template/module/'.$extract_file, file_get_contents($file_path.'admin/view/template/module/'.$extract_file));
 			
 			if(is_file($file_path.'catalog/view/theme/default/template/module/'.$extract_file))
 			$zip->addFromString('catalog/view/theme/default/template/module/'.$extract_file, file_get_contents($file_path.'catalog/view/theme/default/template/module/'.$extract_file)); 			


// 			$zip->addFile($file_path.'catalog/view/theme/default/template/module/'.$extract_file, $extract_file); 			
 			
 		} else { // component extraction
 			$docs_root = $_SERVER['DOCUMENT_ROOT'];
 			
 			$component_name = preg_replace('@(.+)/@','', $file_path); 
 			$file_path = preg_replace('@/(.+)@','', $file_path); 			 
 			$file_path = $docs_root."/".$file_path."/";
 			 
 			$extract_file = $component_name."/".$extract_file;
			
//			var_dump('catalog/controller/'.$extract_file, file_get_contents($file_path.'catalog/controller/'.$extract_file));
//			die;
			
			// Catalog
			if(is_file($file_path.'catalog/controller/'.$extract_file))
 			$zip->addFromString('catalog/controller/'.$extract_file, file_get_contents($file_path.'catalog/controller/'.$extract_file));
 			
 			if(is_file($file_path.'catalog/language/english/'.$extract_file))
 			$zip->addFromString('catalog/language/english/'.$extract_file, file_get_contents($file_path.'catalog/language/english/'.$extract_file));
 			
 			// Admin
 			if(is_file($file_path.'admin/controller/'.$extract_file))
 			$zip->addFromString('admin/controller/'.$extract_file, file_get_contents($file_path.'admin/controller/'.$extract_file));
 			
 			if(is_file($file_path.'admin/model/'.$extract_file))
 			$zip->addFromString('admin/model/'.$extract_file, file_get_contents($file_path.'admin/model/'.$extract_file));
 			
 			if(is_file($file_path.'admin/language/english/'.$extract_file))
 			$zip->addFromString('admin/language/english/'.$extract_file, file_get_contents($file_path.'admin/language/english/'.$extract_file));
 			
 			$base_file_name = $extract_file;
 			$extract_file = str_replace('.php', '_form.tpl', $extract_file); 			
 			if(is_file($file_path.'admin/view/template/'.$extract_file))
 			$zip->addFromString('admin/view/template/'.$extract_file, file_get_contents($file_path.'admin/view/template/'.$extract_file));
 			
 			$extract_file = str_replace('.php', '_list.tpl', $base_file_name); 			
 			if(is_file($file_path.'admin/view/template/'.$extract_file))
 			$zip->addFromString('admin/view/template/'.$extract_file, file_get_contents($file_path.'admin/view/template/'.$extract_file));
 			
 			$extract_file = str_replace('.php', '.tpl', $base_file_name);
 			if(is_file($file_path.'catalog/view/theme/default/template/'.$extract_file))
 			$zip->addFromString('catalog/view/theme/default/template/'.$extract_file, file_get_contents($file_path.'catalog/view/theme/default/template/'.$extract_file)); 			
 		}
 	}
 	
	$zip->close();

	// Download the Zip
	ob_flush();
	header('Content-Type: application/octet-stream');
	header('Content-Disposition: attachment; filename="' . $zipFileName . '"');
	header('Content-Transfer-Encoding: binary');
	readfile($zipArchive);
	exit;
}

if (isset($_POST['download'])) {	
	$zipFileName = 'structure.zip';
	
//	$zipFolder = sys_get_temp_dir(); // must be writable
	$zipArchive = tempnam(sys_get_temp_dir(), '').'.zip';			
	
	$folders = array();
	if(preg_match('@\n@', $file_path)) {
		$file_path = nl2br($file_path);
		$folders = explode('<br />', $file_path);
	} else {
		$folders = array($file_path);
	}
	
	// Create the Zip
	$zip = new ZipArchive();
	$zip->open($zipArchive, ZipArchive :: CREATE);	
	 	
 	foreach($folders as $folder) {
 		$zip->addFromString(trim($folder)."/sample.txt", '');	
 	}
 	 	
	$zip->close();

	// Download the Zip
	ob_flush();
	header('Content-Type: application/octet-stream');
	header('Content-Disposition: attachment; filename="' . $zipFileName . '"');
	header('Content-Transfer-Encoding: binary');
	readfile($zipArchive);
	exit;
}

if (isset($_POST['replace'])) {
	$findText = $_POST['findText'];
	$replaceText = $findText;
	
	$folders = array();
	if(preg_match('@\n@', $file_path)) {
		$file_path = nl2br($file_path);
		$folders = explode('<br />', $file_path);
	} else {
		$folders = array($file_path);
	}
	
	$find_me = array();
	$replace_me = array();
	foreach($folders as $key => $each_replace) {
		$current_replace = explode("|", $each_replace);
		$find_me[$key] = "@".trim($current_replace['0'])."@";	
		$replace_me[$key] = trim($current_replace['1']);	
	}
	
	//print_r($folders);
	//print_r($find_me);
	//print_r($replace_me);
	
	if(!empty($findText)) {
		foreach($folders as $key => $each_replace) {
			$replaceText = preg_replace($find_me[$key], $replace_me[$key], $replaceText);
		} 		
	}
		
	foreach($all_files as $each_file) {
		
		if(count($folders) > 0) {
					
			$get_contents = file_get_contents($each_file);
				
			foreach($folders as $key => $each_replace) {	
				$get_contents = preg_replace($find_me[$key], $replace_me[$key], $get_contents);
			}
			
			$each_file = str_replace('input', 'output', $each_file);
			
			file_put_contents($each_file, $get_contents);
		}	
	}
}
?>
<style>
.even {
	background:white;
}
.odd {
	background:lightgray;
}

textarea, input[type=text] {
	background:white;
	border:1px solid black;
	color:green;
}

.other_options {
	color:gray;
	font-size:12px;
}
</style>
<h1></h1>
<form id="struct" name="struct" method="post" action="#">
<table width="95%" border="0" cellpadding="3" cellspacing="0">
  <tr>
    <td colspan="2"><input type="submit" name="replace" value="Replace" /> <input type="submit" name="download" value="Download" /> <input type="submit" name="extract" value="Extract" /></td>
  </tr>
  <tr>
    <td colspan="2"><textarea name="path" cols="70" rows="7" /><?=$_POST['path'];?></textarea><br />
    	<span style="font-size:smaller;color:gray;">e.g: &lt;findme&gt; | &lt;replaceme&gt;</span></td>
  </tr>
  <tr>
    <td colspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td><textarea name="findText" cols="70" rows="10" /><?=$findText;?></textarea><br />
    	<span style="font-size:smaller;color:gray;">e.g: input</span></td>
    <td><textarea name="replaceText" cols="70" rows="10" /><?=$replaceText;?></textarea><br />
    	<span style="font-size:smaller;color:gray;">e.g: output</span></td>
  </tr>
</table>
</form>